/*
 *  Class name: Validator.java
 *  Version: 1.0
 *  Date: 3.6.2014
 */
package cz.muni.fi.pv168.addressbook.entities.validators;

import java.util.List;
import java.util.function.Predicate;

/**
 *
 * @author Michal Ďuriš
 * @param <E> entity to validate
 */
public abstract class Validator<E> {

    public static final Predicate<Long> CORRECT_ID_PREDICATE = id -> id != null && id > 0;
    public static final Predicate<String> NOT_EMPTY_STRING_PREDICATE = value -> value != null && !value
            .isEmpty();
    public static final Predicate<List<?>> NOT_EMPTY_LIST_PREDICATE = list -> list != null && !list
            .isEmpty();

    public enum Test {

        NULL,
        ID,
        REQUIRED;
    }

    public Validator() {
    }

    public void validate(E entity, Test... tests) throws
            IllegalArgumentException {

        for (Test test : tests) {

            switch (test) {
                case NULL:
                    testToNull(entity);
                    break;
                case ID:
                    testID(entity);
                    break;
                case REQUIRED:
                    testRequiredFields(entity);
                    break;
            }
        }
    }

    protected void testToNull(E entity) throws IllegalArgumentException {
        if (entity == null) {
            throw new IllegalArgumentException();
        }
    }

    protected abstract void testID(E entity) throws IllegalArgumentException;

    protected abstract void testRequiredFields(E entity) throws
            IllegalArgumentException;
}
